package top.yrlby.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import top.yrlby.common.CustomException;
import top.yrlby.pojo.Category;
import top.yrlby.dao.CategoryMapper;
import top.yrlby.pojo.Dish;
import top.yrlby.pojo.Setmeal;
import top.yrlby.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.yrlby.service.DishService;
import top.yrlby.service.SetmealService;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author baiyang
 * @since 2022-09-05
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
        @Autowired
        private DishService dishService;
        @Autowired
        SetmealService setmealService;

        @Override
        public void remove(Long id) {
            if(
                    dishService.count(Wrappers.<Dish>lambdaQuery().eq(Dish::getCategoryId,id))
                            >0
            ){
                throw  new CustomException("当前分类下关联了菜品，不能删除！");
            }
            if(
                    setmealService.count(Wrappers.<Setmeal>lambdaQuery().eq(Setmeal::getCategoryId, id))
                            >0
            ){
                throw  new CustomException("当前分类关联了套餐，不能删除！");
            }
            removeById(id);
        }
}
